package jfoo;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

import database.Database;

public class ControlerWindow extends WindowAdapter implements ActionListener{
    
    
    private Window w;
    private ControlCenter controlCenter;
    
    public ControlerWindow(Window w, ControlCenter controlCenter) {
    	this.controlCenter = controlCenter;
        this.w=w;

    }

    public void actionPerformed(ActionEvent e) {
        
        this.close();
    }
    
    public void windowClosing(WindowEvent e) {
        
        this.close();
        
    }
    
    public void windowIconified(WindowEvent e) {
    	controlCenter.setIconified(true); // to disable the spectrum
    }

    /**
     * Invoked when a window is de-iconified.
     */
    public void windowDeiconified(WindowEvent e) {
    	controlCenter.setIconified(false);
    }

    
    public void close(){
        this.w.setVisible(false);
        ObjectOutputStream fos=null;
         try {
                fos = new ObjectOutputStream(new FileOutputStream("playlists.data"));
                fos.writeObject(w.getPlaylistListModel().getPlaylists());
               
                fos = new ObjectOutputStream(new FileOutputStream("database.data"));
                fos.writeObject(Database.getInstance().getDb());
                
                fos = new ObjectOutputStream(new FileOutputStream("preferences.data"));
                fos.writeObject(Preferences.getInstance().getPrefs());
                
                
            } catch (Exception ex) {
                ex.printStackTrace();
            }
          System.exit(0);  
        
    }
}
